/**
 * validateForm.js
 * v3
 * 4/03/2011
 * 
 *  This file involves validating form for analysis request.
 */
	
	//This function checks validity of inputs for analysis request.
	function validateAnalysisRequest(){
		//Initialize variables
		var current;		
		var i;				
		var flag = 0;	
		
		for(i = 1; i < 9; i++){
			switch (i){
				case 1: current = "SampleCount"; break;
				case 2: current = "ClientName"; break;
				case 3: current = "ClientCompany"; break;
				case 4: current = "ClientAddress"; break;
				case 5: current = "ClientFax"; break;
				case 6: current = "ClientTel"; break;
				case 7: current = "ClientEmail"; break;
				case 8: current = "ClientCell"; break;
			}
			//Calls a function to check if the text field is blank or null.
			if(isBlank(document.getElementById(current).value)){					//If the textfield is blank
				printError(current,0); 
				flag++;
			}
			//Calls a function to check if inputs are valid.
			else if(!verifyField(document.getElementById(current).value,i)){		//If there is an invalid character
				printError(current,1);
				flag++;
			}else printError(current,-1);			//If no error
		}
		
		if(flag == 0) return true;
			
		return false;
	}
	
	//Function that prints error message beside the textfield
	function printError(field,x){
		var str="";
		
			switch(x){
				case 0: str = "This field is required."; break;
				case 1: str = "Invalid Character/s" ; break; 
				default: str = ""; break;
			}
			
			document.getElementById("error"+field).innerHTML = str;
			
	}
	
	//Function that checks if the textfield is null
	function isBlank(fieldValue){	
		if(fieldValue == "" || fieldValue == null){				
			return true;
		}
		return false;
	}
	
	//Funnhecks if there are invalid characters
	function verifyField(fieldValue,x){
		var pattern;
		
		switch(x){		
			case 1: pattern = /(?!^0*$)(?!^0*\.0*$)^\d{1,5}(\.\d{1,2})?$/; break;				//Pattern for PreparedBy
			case 2: pattern = /^[a-zA-Z\s\-.]+$/; break;				//Pattern for ClientName
			case 3: pattern = /^[a-zA-Z\s\-]+$/; break;				//Pattern for ClientName
			case 4: pattern = /^[\,0-9a-zA-Z\s\-]+$/; break;				//Pattern for ClientName
			case 5: pattern = /^[\s\+\-1-9]+$/; break;		//Pattern for ClientTelfax
			case 6: pattern = /^[\s\+\-1-9]+$/; break;			//Pattern for ClientTelfax
			
			//Pattern for ClientTelfax
			case 7: pattern = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*/; break;			
			case 8: pattern = /^[\s\+\-1-9]+$/; break;			//Pattern for ClientTelfax
			
			default: alert("An error occured! Try again.");
		}
		
		if(pattern.test(fieldValue)){
			return true;
		}else return false;
		
		
	}